package com.tanhua.api.impl.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.Answers;
import com.tanhua.domain.db.SoulQuestionOption;
import com.tanhua.mapper.db.SoulQuestionOptionMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Richard Wang
 * @date 2021/9/5 14:50
 */
@Service
public class SoulQuestionOptionApiImpl implements com.tanhua.api.db.SoulQuestionOptionApi {
    @Autowired
    private SoulQuestionOptionMapper soulQuestionOptionMapper;


    @Override

    public Integer checkScore(ArrayList<Answers> answersList) {
        Integer sum=0;
        for (Answers answers : answersList) {
            QueryWrapper<SoulQuestionOption> qr = new QueryWrapper<>();
            qr.eq("id",answers.getOptionId());
            qr.eq("question_id",answers.getQuestionId());
            SoulQuestionOption soulQuestionOption = soulQuestionOptionMapper.selectOne(qr);
            sum+=soulQuestionOption.getScore();
        }
        return sum;
    }

    /**
     * 保存问题选项
     * @param soulQuestionOption
     */
    @Override
    public void save(SoulQuestionOption soulQuestionOption) {
        soulQuestionOptionMapper.insert(soulQuestionOption);
    }

    /**
     * 根据问题 id 查询选项
     * @param questionId
     * @return
     */
    @Override
    public List<SoulQuestionOption> findByQuestionId(Integer questionId) {
        QueryWrapper<SoulQuestionOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id", questionId);

        return soulQuestionOptionMapper.selectList(queryWrapper);
    }


    /**
     * 根据 questionId 删除
     * @param questionId
     */
    @Override
    public void deleteByQuestionId(Integer questionId) {

        QueryWrapper<SoulQuestionOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id", questionId);
        soulQuestionOptionMapper.delete(queryWrapper);
    }
}
